import businessCommonMgr from '@/biz/businessReport/businessCommon'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'
let _this = null
let companys = []
class SearchOpt extends Opt {
  constructor(vm) {
    _this = vm
    super()
    this.build()
  }

  getCompany() {
    let opt = {
      title: '组织筛选',
      key: 'companyIds',
      placeholder: '全部',
      visible: _this.$route.query.type == 'caseReceiver' ? false : true,
      tree: {
        options: companys,
        saveAll: true
      }
    }
    let params = {
      isTwoLevel: true
    }
    businessCommonMgr.getOrgThreeTree(params).then((res) => {
      companys = res
      opt.tree.options = companys
    })
    return opt
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let callTimeChange = {
      title: '回拨时间',
      placeholder: '请选择时间范围',
      key: 'callTime',
      date: {
        type: 'daterange',
        disabled: (date) => {
          let startDate = _this.formValidate.clueGenTime[0] ? _this.formValidate.clueGenTime[0] : ''
          return date.valueOf() < new Date(startDate).getTime()
        }
      },
      data: {
        to: (val) => {
          return {
            callbackStartTime: val[0],
            callbackEndTime: val[1]
          }
        }
      },
      disabled: true
    }
    this.all = [
      this.getCompany(),
      houseOpt.create({
        title: '所属案场',
        placeholder: '请输入所属案场',
        visible: _this.$route.query.type == 'caseReceiver' ? false : true,
        to: (info) => {
          return {
            buildingId: info.housesId
          }
        }
      }),
      {
        title: '线索生成时间',
        placeholder: '请选择时间范围',
        key: 'clueGenTime',
        date: {
          type: 'daterange'
        },
        change: (val) => {
          console.log(val)
          if (val.value[0] || _this.formValidate.batch) {
            callTimeChange.disabled = false
            _this.formValidate.callTime = []
          } else {
            callTimeChange.disabled = true
            _this.formValidate.callTime = []
          }
        }
      },
      callTimeChange,
      {
        title: '线索投放渠道',
        key: 'channelType',
        select: {
          options: [
            {
              key: '0',
              value: '全部'
            },
            {
              key: '1',
              value: '同策好房'
            },
            {
              key: '2',
              value: '百度地图'
            }
          ]
        }
      }
    ]
  }
}

export default SearchOpt
